﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TheHeGeoDao;

namespace TheHeGeoManager.Util
{
    public class SensorAlertHelper
    {
        public Dictionary<string, SensorAlert> DictSensor { get; protected set; }
        public Dictionary<string, NguoiSuDung> DictNsd { get; protected set; }

        public List<PhanQuyenDuAn> ListtPhanQuyen { get; protected set; } 

        public SensorAlertHelper()
        {
            GetDictNsd();
            GetDictSensor();
        }
        
        public void AddSensorValue(string maSensor, double value)
        {
            if (DictSensor.ContainsKey(maSensor))
            {
                DictSensor[maSensor].SetValue(value);
            }
        }

        public void GetDictNsd()
        {
            DictNsd = DaoHelper.Entities.NguoiSuDungs.ToDictionary(p => p.UserName, p => p);
            ListtPhanQuyen = DaoHelper.Entities.PhanQuyenDuAns.ToList();
        }

        public void GetDictSensor()
        {
            DictSensor = (from s in DaoHelper.Entities.Sensors
                join t in DaoHelper.Entities.ThietBis on s.MaThietBi equals t.MaThietBi
                join d in DaoHelper.Entities.DuAns on t.MaDuAn equals d.MaDuAn
                where t.Status == 1
                select new SensorAlert
                {
                   ThoiGianLayMauCanhBao = !s.ThoiGianLayMauCanhBao.HasValue ? 0 : s.ThoiGianLayMauCanhBao.Value,
                   GioiHanCanhBao = !s.GioiHanCanhBao.HasValue?0:s.GioiHanCanhBao.Value,
                   MaSensor = s.MaSensor,
                   TenSensor = s.TenSensor,
                   MaThietBi = t.MaThietBi,
                   TenThietBi = t.TenThietBi,
                   MaDuAn = d.MaDuAn,
                   TenDuAn = d.TenDuAn,
                }).ToDictionary(p=>p.MaSensor,p=>p);
        }

        public void UpdateNguongCanhBao(string maSensor,int thoiGianLayMau, double gioiHanCanhBao)
        {
            if (DictSensor.ContainsKey(maSensor))
            {
                var sensor = DictSensor[maSensor];
                sensor.ThoiGianLayMauCanhBao = thoiGianLayMau;
                sensor.GioiHanCanhBao = gioiHanCanhBao;
            }
        }
    }
}
